package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.utils.FileUploadUtils;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author 李旭
 * @date 2022/1/4 11:12
 * @Description:
 *
 * 上传文件
 *
 */
@RestController
@RequestMapping("/admin/product")
@RefreshScope //扩展配置文件一旦发生变化 马上更新
public class FileController {

    @Value("${image.url}")
    private String imageUrl;

    @Autowired
    private FileUploadUtils fileUploadUtils;

    //上传文件
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file) throws Exception{

        System.out.println(file.getSize());
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        //System.out.println(file.getBytes());
        String path = fileUploadUtils.upload(file.getOriginalFilename(), file.getBytes());
        System.out.println(imageUrl + path);
        return Result.ok(imageUrl + path);
    }

    //  获取文件上传对应的地址
    @Value("${minio.endpointUrl}")
    public String endpointUrl;

    @Value("${minio.accessKey}")
    public String accessKey;

    @Value("${minio.secreKey}")
    public String secreKey;

    @Value("${minio.bucketName}")
    public String bucketName;
/*
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file) throws Exception{

        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpointUrl)
                .credentials(accessKey,secreKey).build();

        //判断是否有桶
        boolean bucketExists = minioClient.
                bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if(bucketExists){
            System.out.println(bucketName + "桶已经存在了");
        }else{
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }

        String fileName = System.currentTimeMillis() + UUID.randomUUID().toString();

        minioClient.putObject(PutObjectArgs.builder()
                 .bucket(bucketName)
                 .object(fileName)
                 .stream(file.getInputStream(),file.getSize(),-1)
                 .contentType(file.getContentType()).build());

        String url = endpointUrl + "/" + bucketName + "/" + fileName;
        System.out.println(url);
        return Result.ok(url);
    }*/

}
